package pl.edu.pk.mech.kkbus.controller;

import java.util.List;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import pl.edu.pk.mech.kkbus.model.DepartureTime;
import pl.edu.pk.mech.kkbus.model.ResponseStatus;
import pl.edu.pk.mech.kkbus.service.DepartureService;

@Controller
public class DepartureController extends RestController{

	private static final Logger log = Logger.getLogger(UserController.class.getName());
	
	@Autowired
	DepartureService departureService;
	
    @RequestMapping(value = "/departures", method = RequestMethod.GET)
    public @ResponseBody List<DepartureTime> getDepartures(@RequestParam(required = false, defaultValue = "-1") int page) {
        if (page < 0) {
            log.info("Get all departures");
            return departureService.getAllDepartures();
        } else {        
            log.info("Get part departure. Page: " + page);
            return null;
        }         
    } 
    
    @RequestMapping(value = "/departure", method = RequestMethod.GET)
    public @ResponseBody DepartureTime getDeparture(@RequestParam(required = false, defaultValue = "-1") long id) {
            log.info("Get departure");
            return departureService.getDeparture(id);      
    } 
    
    @RequestMapping(value = "/departure", method = RequestMethod.POST)
    public @ResponseBody ResponseStatus createDeparture(@RequestBody DepartureTime departureTime) {
    	departureService.createOrUpdateDeparture(departureTime);
        return ResponseStatus.ADDED;
    }
}
